package hospital.order.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hospital.common.helper.HttpRequestHelper;
import hospital.enums.OrderStatusEnum;
import hospital.enums.PaymentStatusEnum;
import hospital.enums.PaymentTypeEnum;
import hospital.hospClient.HospFeignClient;
import hospital.model.order.OrderInfo;
import hospital.model.order.PaymentInfo;
import hospital.order.mapper.PaymentMapper;
import hospital.order.service.OrderService;
import hospital.order.service.PaymentService;
import hospital.vo.order.SignInfoVo;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @description:
 */
@Service
public class PaymentServiceImpl extends ServiceImpl<PaymentMapper, PaymentInfo> implements PaymentService {

    @Autowired
    private OrderService orderService;
    @Autowired
    private HospFeignClient hospFeignClient;
    /**
     * @Author yang
     * @Description //TODO 向支付记录表添加信息
     * @Param [orderInfo, weixin]
     * @return
     **/
    @Override
    public void savePaymentInfo(OrderInfo orderInfo, Integer paymentType) {
        //1.根据订单号和支付类型，判断数据库中是否有此订单，如果有则返回，没有则创建
        QueryWrapper<PaymentInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("order_id",orderInfo.getOutTradeNo());
        wrapper.eq("payment_type",paymentType);

        //查询数据库
        Integer count = baseMapper.selectCount(wrapper);
        //如果有则返回
        if(count>0){
            return;
        }

        //没有则添加
        //添加记录
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setCreateTime(new Date());
        paymentInfo.setOrderId(orderInfo.getId());
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOutTradeNo(orderInfo.getOutTradeNo()); //对外交易号
        paymentInfo.setTradeNo(orderInfo.getOutTradeNo());  //订单编号
        paymentInfo.setPaymentStatus(PaymentStatusEnum.UNPAID.getStatus());
        String subject = new DateTime(orderInfo.getReserveDate()).toString("yyyy-MM-dd")+"|"+orderInfo.getHosname()+"|"+orderInfo.getDepname()+"|"+orderInfo.getTitle();
        paymentInfo.setSubject(subject);
        paymentInfo.setTotalAmount(orderInfo.getAmount());
        baseMapper.insert(paymentInfo);
    }

    /**
     * @Author yang
     * @Description //TODO //更新订单表，支付表，医院的状态
     * @Param [out_trade_no, resultMap]
     * @return
     **/
    @Override
    public void paySuccess(String out_trade_no, Map<String, String> resultMap) {
        //1. 查询出支付表中的记录，更新里面的状态
        QueryWrapper<PaymentInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("out_trade_no",out_trade_no);
        wrapper.eq("payment_type",PaymentTypeEnum.WEIXIN.getStatus());
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);

        //更新记录
        //回调时间
        paymentInfo.setCallbackTime(new Date());
        //微信订单号
        paymentInfo.setTradeNo(resultMap.get("transaction_id"));
        //回调信息
        paymentInfo.setCallbackContent(resultMap.toString());
        //更新支付状态
        paymentInfo.setPaymentStatus(PaymentStatusEnum.PAID.getStatus());
        baseMapper.updateById(paymentInfo);

        //2. 更新订单表的支付状态
        OrderInfo orderInfo = orderService.getById(paymentInfo.getOrderId());
        orderInfo.setOrderStatus(OrderStatusEnum.PAID.getStatus());
        orderService.updateById(orderInfo);

        //3.调用医院接口，更新订单支付信息
        SignInfoVo signInfoVo = hospFeignClient.getSignInfoVo(orderInfo.getHoscode());
        Map<String,Object> reqMap = new HashMap<>();
        reqMap.put("hoscode",orderInfo.getHoscode());
        reqMap.put("hosRecordId",orderInfo.getHosRecordId());
        reqMap.put("timestamp", HttpRequestHelper.getTimestamp());
        String sign = HttpRequestHelper.getSign(reqMap, signInfoVo.getSignKey());
        reqMap.put("sign", sign);

        HttpRequestHelper.sendRequest(reqMap,signInfoVo.getApiUrl()+"/order/updatePayStatus");
    }

    /**
     * @Author yang
     * @Description //TODO //获取支付记录信息
     * @Param [orderId, status]
     * @return
     **/
    @Override
    public PaymentInfo getPaymentByOrderId(Long orderId, Integer status) {
        //1. 查询出支付表中的记录，更新里面的状态
        QueryWrapper<PaymentInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        wrapper.eq("payment_type",PaymentTypeEnum.WEIXIN.getStatus());
        PaymentInfo paymentInfo = baseMapper.selectOne(wrapper);
        return paymentInfo;
    }
}
